//
//  UserData.m
//  StoneMason
//
//  Created by iOS1 on 16/12/5.
//  Copyright © 2016年 iOS1. All rights reserved.
//

#import "UserData.h"
#import "SMUtil.h"

#define userInfoKey  @"userInfo"
#define loginDataKey @"loginData"

@implementation UserData

+(instancetype)instance{
    static UserData *user = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        user = [[UserData alloc]init];
        user.loginState = [[NSUserDefaults standardUserDefaults]boolForKey:@"isLogin"];
    });
    return user;
}

-(instancetype)init{
    if (self = [super init]) {
        [self loadUserData];
    }
    return self;
}

-(void)loadUserData{
    if (userInfo) {
        return ;
    }
    NSDictionary *info = [[NSUserDefaults standardUserDefaults]objectForKey:userInfoKey];
    if (info) {
        userInfo = [[UserInfoModel alloc]init];
        [userInfo setValuesForKeysWithDictionary:info];
    }
}

-(void)setLegalItemsH5Url:(NSString *)legalItemsH5Url{
    _legalItemsH5Url = legalItemsH5Url;
    [[NSUserDefaults standardUserDefaults] setObject:legalItemsH5Url forKey:@"legalItemsH5Url"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

-(NSString *)getLegalItemsH5Url{
    if (_legalItemsH5Url) {
        return _legalItemsH5Url;
    }
    return [[NSUserDefaults standardUserDefaults]objectForKey:@"legalItemsH5Url"];
}

-(void)setPassword:(NSString *)password{
    _password = password;
    [[NSUserDefaults standardUserDefaults] setObject:password forKey:@"password"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

-(NSString *)getPassword{
    if (_password) {
        return _password;
    }
    return [[NSUserDefaults standardUserDefaults]objectForKey:@"password"];
}

-(void)setLoginState:(BOOL)loginState{
    _loginState = loginState;
    [[NSUserDefaults standardUserDefaults] setBool:loginState forKey:@"isLogin"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"loginStateChanged" object:nil];
    if (self.loginStateChanged) {
        self.loginStateChanged();
    }
    if (!loginState) {
        UserInfoModel *userInfoModel = [[UserInfoModel alloc]init];
        [self updateUserInfo:userInfoModel];
    }
}

-(BOOL)getLoginState{
    if (_loginState) {
        return _loginState;
    }
    return [[NSUserDefaults standardUserDefaults]boolForKey:@"isLogin"];
}
#pragma mark - 取

-(UserInfoModel *)getUserInfo{
    return userInfo;
}

//从网络获取数据
-(void)refreshFromNet {
    __weak typeof(self)weakSelf = self;
    [SMUtil requestWithMethod:NetWorkFunctionPost with:UserInfo_Url parameters:nil success:^(id data) {
        UserInfoModel *infoModel = [[UserData instance] getUserInfo];
//        NSDictionary *dict = [infoModel.defaultAddress copy];
        [infoModel setValuesForKeysWithDictionary:data[0]];
//        infoModel.defaultAddress = dict;
        infoModel.token = [self getUserInfo].token;
        [weakSelf updateUserInfo:infoModel];
        
        if (weakSelf.refreshSuccess) {
            weakSelf.refreshSuccess();
        }
    } faild:^(NSString *msg, int code) {
        
    }];
}

BOOL isLogin(){
    return [[UserData instance] getLoginState];
}

- (void)setAutoLogin:(BOOL)autoLogin{
    if (autoLogin == YES) {
        if (isLogin()) {
            
        }
    }
}

#pragma mark - 存
-(void)save{
    if (userInfo) {
        [[NSUserDefaults standardUserDefaults] setObject:[userInfo toDictionary] forKey:userInfoKey];
    }
    [[NSUserDefaults standardUserDefaults] synchronize];
}

-(void)clearLoginData{
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:userInfoKey];
    [[NSUserDefaults standardUserDefaults] synchronize];
}


-(void)updateUserInfo:(UserInfoModel *)user{
    userInfo = user;
    [self save];
}

- (void)setValue:(id)value forUndefinedKey:(NSString *)key{
    
}

@end


@implementation UserInfoModel
- (void)setValue:(id)value forUndefinedKey:(NSString *)key{
    if ([key isEqualToString:@"id"]) {
        self.uid = value;
    }
}
@end
